/*
 * XProgressDialog.java
 * 
 * Copyright (c) 2006 - 2007
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.xfc.dialog;

import java.awt.Font;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JProgressBar;

import org.xfc.XApp;
import org.xfc.XAppFrame;
import org.xfc.components.XIconButton;
import org.xfc.components.XMultilineLabel;

import com.jgoodies.forms.layout.FormLayout;

// TODO: clean the look of this dialog up a bit -


/**
 * 
 * 
 * @author Devon Carew
 */
public class XProgressDialog
	extends XDialog
{
	private static Icon DEFAULT_ICON = XApp.getApp().getResources().getIcon("/eclipse/org.eclipse.ui/icons/full/progress/waiting.gif");
	
	private JLabel			iconLabel = new JLabel(DEFAULT_ICON);
	private JLabel			textLabel = new JLabel();
	private XMultilineLabel	descriptionLabel = new XMultilineLabel();
	private JProgressBar	progressBar = new JProgressBar();
	private	XIconButton		stopButton;
	private ActionListener	cancelListener;
	
	public XProgressDialog()
	{
		this(XApp.getApp());
	}
	
	public XProgressDialog(XApp app)
	{
		this(app.getActiveAppFrame());
	}
	
	public XProgressDialog(XAppFrame appFrame)
	{
		super(appFrame);
		
		setTitle("Task Progress");
		setResizable(false);
		
		initUI();
	}
	
	private void initUI()
	{
		JPanel content = new JPanel(new FormLayout(
			"pref 5dlu pref:grow 2dlu pref",
			"pref 0dlu pref 8dlu pref"
		));
		
		stopButton = new XIconButton(
			XApp.getApp().getResources().getIcon("/eclipse/org.eclipse.ui/icons/full/elcl16/progress_stop.gif"));
		stopButton.setToolTipText("Cancel");
		
		textLabel.setFont(textLabel.getFont().deriveFont(Font.BOLD));
		descriptionLabel.setBorder(BorderFactory.createEmptyBorder(0, 12, 0, 0));
		descriptionLabel.setPreferredWidth(250);
		
		stopButton.setEnabled(false);
		
		content.add(iconLabel, 						"1, 1, 1, 3, l, t");
		content.add(textLabel, 						"3, 1, 3, 1, l, b");
		content.add(descriptionLabel, 				"3, 3, 3, 1");
		content.add(Box.createHorizontalStrut(200),	"4, 3");
		content.add(progressBar, 					"3, 5");
		content.add(stopButton, 					"5, 5");
		
		setContents(content);
		
		construct(false);
	}
	
	public void setText(String text)
	{
		setTitle(text);
		textLabel.setText(text);
	}
	
	public String getText()
	{
		return textLabel.getText();
	}
	
	public void setDescription(String description)
	{
		descriptionLabel.setText(description);
	}
	
	public String getDescription()
	{
		return descriptionLabel.getText();
	}
	
	public void setIcon(Icon icon)
	{
		iconLabel.setIcon(icon);
	}
	
	public Icon getIcon()
	{
		return iconLabel.getIcon();
	}
	
	public JProgressBar getProgressBar()
	{
		return progressBar;
	}
	
	public void setCancelListener(ActionListener cancelListener)
	{
		if (this.cancelListener != null)
			stopButton.removeActionListener(this.cancelListener);
		
		this.cancelListener = cancelListener;
		
		if (this.cancelListener != null)
			stopButton.addActionListener(this.cancelListener);
		
		stopButton.setEnabled(this.cancelListener != null);
	}
	
}
